<?php
/**
 * author:歪妻
 * Date: 2020/11/2 0002
 */

require_once 'lib/common.php';
session_start();

// 连接到数据库，运行查询，处理错误
$pdo = getPDO();
$stmt = $pdo->query(
    'SELECT
       id,title, created_at, body
    FROM
        post
    ORDER BY
        created_at DESC'
);
if ($stmt === false)
{
    throw new Exception('运行此查询时出现问题');
}
$notFound = isset($_GET['not-found']);
?>

<!DOCTYPE html>
<html>
<head>
    <title>A blog application</title>
    <?php require 'templates/head.php' ?></head>
<body>
<?php require 'templates/title.php' ?>
<?php if ($notFound): ?>
    <div class="error box">
        错误: 找不到请求的博客文章
    </div>
<?php endif ?>
<div class="post-list">
    <?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
        <div class="post-synopsis">
            <h2>
                <?php echo htmlEscape($row['title']) ?>
            </h2>
            <div class="meta">
                <?php echo convertSqlDate($row['created_at']) ?>
                (<?php echo countCommentsForPost($pdo,$row['id']) ?>  评论)
            </div>
            <p>
                <?php echo htmlEscape($row['body']) ?>
            </p>
           <div class="read-more">
               <a href="view-post.php?post_id=<?php echo $row['id']?>">Read more...</a>
           </div>
        </div>
    <?php endwhile ?>
</div>
</body>
</html>
